{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 39,
   "id": "c0b720c1-8f6d-4e50-935a-678cd9442308",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-03-29T15:59:16.507204Z",
     "iopub.status.busy": "2022-03-29T15:59:16.506642Z",
     "iopub.status.idle": "2022-03-29T15:59:19.400450Z",
     "shell.execute_reply": "2022-03-29T15:59:19.399617Z",
     "shell.execute_reply.started": "2022-03-29T15:59:16.507166Z"
    },
    "scrolled": true,
    "tags": []
   },
   "outputs": [],
   "source": [
    "from pathlib import Path  #pathlib官方定义是面向对象的文件系统路径，需要python3.4+\n",
    "import os\n",
    "import gzip\n",
    "import shutil\n",
    "\n",
    "os.mkdir('/home/aistudio/123/111')  #创建输出文件夹\n",
    "mypath = \"/home/aistudio/123\"       #mypath是源文件夹，包含多级子目录及目录下的.gz文件\n",
    "for p in Path(mypath).iterdir():    #通过pathlib模块遍历mypath下的文件夹及文件，赋值给p，即p有了mypath下的文件信息。\n",
    "    for s in p.rglob('*.gz'):       #也是pathlib中的功能，在p中遍历中所有带.gz的文件，赋值给s，作为输入\n",
    "        y = str(s)                  #改变变量类型，赋值给y。不改变类型直接用s.replace会报错！\n",
    "        t = y.replace(\".gz\", \"\")    #删去文件y中的.gz，赋值给t，作为输出\n",
    "        with gzip.open(s, 'r') as f_in, open(t, 'wb') as f_out:        #用gzip打开输入s as f_in，输出t as f_out。详情参考python中的gzip语句的使用\n",
    "            shutil.copyfileobj(f_in, f_out)                            #用shutil把f_in和f_out写在每个.gz的当前文件夹\n",
    "            shutil.move(t,'/home/aistudio/123/111')                    #把输出文件移动到你的输出文件夹\n",
    "\n",
    "#代码目的是将源路径下多级子目录内的文件统一操作，并复制输出文件至指定文件夹。只是完成了功能，可以看出还有很多可以简洁、优化的地方.."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "id": "2621400c-2f92-4620-981e-b248bedd10b3",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-03-29T15:54:17.887278Z",
     "iopub.status.busy": "2022-03-29T15:54:17.886851Z",
     "iopub.status.idle": "2022-03-29T15:54:17.919653Z",
     "shell.execute_reply": "2022-03-29T15:54:17.918999Z",
     "shell.execute_reply.started": "2022-03-29T15:54:17.887245Z"
    },
    "scrolled": true,
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "/home/aistudio/123/AA/EDML/AAEDML.xaa.sdf\n",
      "/home/aistudio/123/AA/BDMN/AABDMN.xaa.sdf\n",
      "/home/aistudio/123/AA/ABMP/AAABMP.xaa.sdf\n",
      "/home/aistudio/123/AA/EBMN/AAEBMN.xaa.sdf\n",
      "/home/aistudio/123/AA/BDMM/AABDMM.xaa.sdf\n",
      "/home/aistudio/123/AA/ADML/AAADML.xaa.sdf\n",
      "/home/aistudio/123/AA/BARO/AABARO.xaa.sdf\n",
      "/home/aistudio/123/AA/BBRN/AABBRN.xaa.sdf\n",
      "/home/aistudio/123/AA/EDRN/AAEDRN.xaa.sdf\n",
      "/home/aistudio/123/AA/EARO/AAEARO.xaa.sdf\n",
      "/home/aistudio/123/AA/BDRO/AABDRO.xaa.sdf\n",
      "/home/aistudio/123/AA/BAMN/AABAMN.xaa.sdf\n",
      "/home/aistudio/123/AA/ADRP/AAADRP.xaa.sdf\n",
      "/home/aistudio/123/AA/BARN/AABARN.xaa.sdf\n",
      "/home/aistudio/123/AA/EDRL/AAEDRL.xaa.sdf\n",
      "/home/aistudio/123/AA/BBRO/AABBRO.xaa.sdf\n",
      "/home/aistudio/123/AA/ADRM/AAADRM.xaa.sdf\n",
      "/home/aistudio/123/AA/EBRN/AAEBRN.xaa.sdf\n",
      "/home/aistudio/123/AA/BDRM/AABDRM.xaa.sdf\n",
      "/home/aistudio/123/AA/ABRP/AAABRP.xaa.sdf\n",
      "/home/aistudio/123/AA/ACRN/AAACRN.xaa.sdf\n",
      "/home/aistudio/123/AA/ABMO/AAABMO.xaa.sdf\n",
      "/home/aistudio/123/AA/ADMP/AAADMP.xaa.sdf\n",
      "/home/aistudio/123/AA/ABRM/AAABRM.xaa.sdf\n",
      "/home/aistudio/123/AA/EAMP/AAEAMP.xaa.sdf\n",
      "/home/aistudio/123/AA/BBRP/AABBRP.xaa.sdf\n",
      "/home/aistudio/123/AA/CARO/AACARO.xaa.sdf\n",
      "/home/aistudio/123/AA/EBRM/AAEBRM.xaa.sdf\n",
      "/home/aistudio/123/AA/EBRL/AAEBRL.xaa.sdf\n",
      "/home/aistudio/123/AA/CBMN/AACBMN.xaa.sdf\n",
      "/home/aistudio/123/AA/ABRL/AAABRL.xaa.sdf\n",
      "/home/aistudio/123/AA/ABML/AAABML.xaa.sdf\n",
      "/home/aistudio/123/AA/EDRO/AAEDRO.xaa.sdf\n",
      "/home/aistudio/123/AA/AARM/AAAARM.xaa.sdf\n",
      "/home/aistudio/123/AA/CARM/AACARM.xaa.sdf\n",
      "/home/aistudio/123/AA/ADMM/AAADMM.xaa.sdf\n",
      "/home/aistudio/123/AA/ABMM/AAABMM.xaa.sdf\n",
      "/home/aistudio/123/AA/BAMO/AABAMO.xaa.sdf\n",
      "/home/aistudio/123/AA/EARM/AAEARM.xaa.sdf\n",
      "/home/aistudio/123/AA/AARN/AAAARN.xaa.sdf\n",
      "/home/aistudio/123/AA/ABRN/AAABRN.xaa.sdf\n",
      "/home/aistudio/123/AA/EDRM/AAEDRM.xaa.sdf\n",
      "/home/aistudio/123/AA/ADRO/AAADRO.xaa.sdf\n",
      "/home/aistudio/123/AA/EDMO/AAEDMO.xaa.sdf\n",
      "/home/aistudio/123/AA/CBRM/AACBRM.xaa.sdf\n",
      "/home/aistudio/123/AA/CARL/AACARL.xaa.sdf\n",
      "/home/aistudio/123/AA/EDRP/AAEDRP.xaa.sdf\n",
      "/home/aistudio/123/AA/AAMP/AAAAMP.xaa.sdf\n",
      "/home/aistudio/123/AA/ADMO/AAADMO.xaa.sdf\n",
      "/home/aistudio/123/AA/EDMP/AAEDMP.xaa.sdf\n",
      "/home/aistudio/123/AA/BBMN/AABBMN.xaa.sdf\n",
      "/home/aistudio/123/AA/ABRO/AAABRO.xaa.sdf\n",
      "/home/aistudio/123/AA/EARL/AAEARL.xaa.sdf\n",
      "/home/aistudio/123/AA/EAMO/AAEAMO.xaa.sdf\n",
      "/home/aistudio/123/AA/CBRN/AACBRN.xaa.sdf\n",
      "/home/aistudio/123/AA/CARN/AACARN.xaa.sdf\n",
      "/home/aistudio/123/AA/EBMM/AAEBMM.xaa.sdf\n",
      "/home/aistudio/123/AA/CAMM/AACAMM.xaa.sdf\n",
      "/home/aistudio/123/AA/EDMM/AAEDMM.xaa.sdf\n",
      "/home/aistudio/123/AA/AARO/AAAARO.xaa.sdf\n",
      "/home/aistudio/123/AA/AAMM/AAAAMM.xaa.sdf\n",
      "/home/aistudio/123/AA/BARM/AABARM.xaa.sdf\n",
      "/home/aistudio/123/AA/CDRL/AACDRL.xaa.sdf\n",
      "/home/aistudio/123/AA/ADRN/AAADRN.xaa.sdf\n",
      "/home/aistudio/123/AA/ADRL/AAADRL.xaa.sdf\n",
      "/home/aistudio/123/AA/EBMO/AAEBMO.xaa.sdf\n",
      "/home/aistudio/123/AA/EAML/AAEAML.xaa.sdf\n",
      "/home/aistudio/123/AA/BAMM/AABAMM.xaa.sdf\n",
      "/home/aistudio/123/AA/ADMN/AAADMN.xaa.sdf\n",
      "/home/aistudio/123/AA/EARP/AAEARP.xaa.sdf\n",
      "/home/aistudio/123/AA/EARN/AAEARN.xaa.sdf\n",
      "/home/aistudio/123/AA/AAMN/AAAAMN.xaa.sdf\n",
      "/home/aistudio/123/AA/BBMO/AABBMO.xaa.sdf\n",
      "/home/aistudio/123/AA/EBRO/AAEBRO.xaa.sdf\n",
      "/home/aistudio/123/AA/AARP/AAAARP.xaa.sdf\n",
      "/home/aistudio/123/AA/EAMN/AAEAMN.xaa.sdf\n",
      "/home/aistudio/123/AA/CARP/AACARP.xaa.sdf\n",
      "/home/aistudio/123/AA/EDMN/AAEDMN.xaa.sdf\n",
      "/home/aistudio/123/AA/BDMO/AABDMO.xaa.sdf\n",
      "/home/aistudio/123/AA/BDRN/AABDRN.xaa.sdf\n",
      "/home/aistudio/123/AA/AAMO/AAAAMO.xaa.sdf\n",
      "/home/aistudio/123/AA/EBRP/AAEBRP.xaa.sdf\n",
      "/home/aistudio/123/AA/CAMN/AACAMN.xaa.sdf\n",
      "/home/aistudio/123/AA/ABMN/AAABMN.xaa.sdf\n",
      "/home/aistudio/123/AA/BBMM/AABBMM.xaa.sdf\n",
      "/home/aistudio/123/AA/CBRO/AACBRO.xaa.sdf\n",
      "/home/aistudio/123/AA/EAMM/AAEAMM.xaa.sdf\n",
      "/home/aistudio/123/AA/BARL/AABARL.xaa.sdf\n",
      "/home/aistudio/123/111/AAAAML.xaa.sdf\n",
      "/home/aistudio/123/BA/BBRM/BABBRM.xaa.sdf\n",
      "/home/aistudio/123/BA/CBMM/BACBMM.xaa.sdf\n",
      "/home/aistudio/123/BA/EDML/BAEDML.xaa.sdf\n",
      "/home/aistudio/123/BA/BDMN/BABDMN.xaa.sdf\n",
      "/home/aistudio/123/BA/ABMP/BAABMP.xaa.sdf\n",
      "/home/aistudio/123/BA/EBMN/BAEBMN.xaa.sdf\n",
      "/home/aistudio/123/BA/BDMM/BABDMM.xaa.sdf\n",
      "/home/aistudio/123/BA/ADML/BAADML.xaa.sdf\n",
      "/home/aistudio/123/BA/BARO/BABARO.xaa.sdf\n",
      "/home/aistudio/123/BA/BBRN/BABBRN.xaa.sdf\n",
      "/home/aistudio/123/BA/EDRN/BAEDRN.xaa.sdf\n",
      "/home/aistudio/123/BA/EARO/BAEARO.xaa.sdf\n",
      "/home/aistudio/123/BA/BDRO/BABDRO.xaa.sdf\n",
      "/home/aistudio/123/BA/BAMN/BABAMN.xaa.sdf\n",
      "/home/aistudio/123/BA/ADRP/BAADRP.xaa.sdf\n",
      "/home/aistudio/123/BA/BARN/BABARN.xaa.sdf\n",
      "/home/aistudio/123/BA/EDRL/BAEDRL.xaa.sdf\n",
      "/home/aistudio/123/BA/BBRO/BABBRO.xaa.sdf\n",
      "/home/aistudio/123/BA/ADRM/BAADRM.xaa.sdf\n",
      "/home/aistudio/123/BA/EBRN/BAEBRN.xaa.sdf\n",
      "/home/aistudio/123/BA/BDRM/BABDRM.xaa.sdf\n",
      "/home/aistudio/123/BA/ABRP/BAABRP.xaa.sdf\n",
      "/home/aistudio/123/BA/BARP/BABARP.xaa.sdf\n",
      "/home/aistudio/123/BA/ABMO/BAABMO.xaa.sdf\n",
      "/home/aistudio/123/BA/ADMP/BAADMP.xaa.sdf\n",
      "/home/aistudio/123/BA/ABRM/BAABRM.xaa.sdf\n",
      "/home/aistudio/123/BA/BBRP/BABBRP.xaa.sdf\n",
      "/home/aistudio/123/BA/CDRN/BACDRN.xaa.sdf\n",
      "/home/aistudio/123/BA/CARO/BACARO.xaa.sdf\n",
      "/home/aistudio/123/BA/EBRM/BAEBRM.xaa.sdf\n",
      "/home/aistudio/123/BA/EBRL/BAEBRL.xaa.sdf\n",
      "/home/aistudio/123/BA/CBMN/BACBMN.xaa.sdf\n",
      "/home/aistudio/123/BA/ABRL/BAABRL.xaa.sdf\n",
      "/home/aistudio/123/BA/ABML/BAABML.xaa.sdf\n",
      "/home/aistudio/123/BA/EDRO/BAEDRO.xaa.sdf\n",
      "/home/aistudio/123/BA/AARM/BAAARM.xaa.sdf\n",
      "/home/aistudio/123/BA/CARM/BACARM.xaa.sdf\n",
      "/home/aistudio/123/BA/ADMM/BAADMM.xaa.sdf\n",
      "/home/aistudio/123/BA/ABMM/BAABMM.xaa.sdf\n",
      "/home/aistudio/123/BA/CAMO/BACAMO.xaa.sdf\n",
      "/home/aistudio/123/BA/BAMO/BABAMO.xaa.sdf\n",
      "/home/aistudio/123/BA/EARM/BAEARM.xaa.sdf\n",
      "/home/aistudio/123/BA/AARN/BAAARN.xaa.sdf\n",
      "/home/aistudio/123/BA/ABRN/BAABRN.xaa.sdf\n",
      "/home/aistudio/123/BA/EDRM/BAEDRM.xaa.sdf\n",
      "/home/aistudio/123/BA/EBML/BAEBML.xaa.sdf\n",
      "/home/aistudio/123/BA/ADRO/BAADRO.xaa.sdf\n",
      "/home/aistudio/123/BA/EBMP/BAEBMP.xaa.sdf\n",
      "/home/aistudio/123/BA/EDMO/BAEDMO.xaa.sdf\n",
      "/home/aistudio/123/BA/CBRM/BACBRM.xaa.sdf\n",
      "/home/aistudio/123/BA/AARL/BAAARL.xaa.sdf\n",
      "/home/aistudio/123/BA/CARL/BACARL.xaa.sdf\n",
      "/home/aistudio/123/BA/EDRP/BAEDRP.xaa.sdf\n",
      "/home/aistudio/123/BA/AAMP/BAAAMP.xaa.sdf\n",
      "/home/aistudio/123/BA/ADMO/BAADMO.xaa.sdf\n",
      "/home/aistudio/123/BA/EDMP/BAEDMP.xaa.sdf\n",
      "/home/aistudio/123/BA/ABRO/BAABRO.xaa.sdf\n",
      "/home/aistudio/123/BA/EARL/BAEARL.xaa.sdf\n",
      "/home/aistudio/123/BA/EAMO/BAEAMO.xaa.sdf\n",
      "/home/aistudio/123/BA/CBRN/BACBRN.xaa.sdf\n",
      "/home/aistudio/123/BA/CARN/BACARN.xaa.sdf\n",
      "/home/aistudio/123/BA/EBMM/BAEBMM.xaa.sdf\n",
      "/home/aistudio/123/BA/CAMM/BACAMM.xaa.sdf\n",
      "/home/aistudio/123/BA/EDMM/BAEDMM.xaa.sdf\n",
      "/home/aistudio/123/BA/AARO/BAAARO.xaa.sdf\n",
      "/home/aistudio/123/BA/AAMM/BAAAMM.xaa.sdf\n",
      "/home/aistudio/123/BA/BARM/BABARM.xaa.sdf\n",
      "/home/aistudio/123/BA/ADRN/BAADRN.xaa.sdf\n",
      "/home/aistudio/123/BA/ADRL/BAADRL.xaa.sdf\n",
      "/home/aistudio/123/BA/EBMO/BAEBMO.xaa.sdf\n",
      "/home/aistudio/123/BA/BAMM/BABAMM.xaa.sdf\n",
      "/home/aistudio/123/BA/ADMN/BAADMN.xaa.sdf\n",
      "/home/aistudio/123/BA/EARP/BAEARP.xaa.sdf\n",
      "/home/aistudio/123/BA/EARN/BAEARN.xaa.sdf\n",
      "/home/aistudio/123/BA/CBRL/BACBRL.xaa.sdf\n",
      "/home/aistudio/123/BA/AAMN/BAAAMN.xaa.sdf\n",
      "/home/aistudio/123/BA/EBRO/BAEBRO.xaa.sdf\n",
      "/home/aistudio/123/BA/BAML/BABAML.xaa.sdf\n",
      "/home/aistudio/123/BA/AARP/BAAARP.xaa.sdf\n",
      "/home/aistudio/123/BA/EAMN/BAEAMN.xaa.sdf\n",
      "/home/aistudio/123/BA/BBML/BABBML.xaa.sdf\n",
      "/home/aistudio/123/BA/BBRL/BABBRL.xaa.sdf\n",
      "/home/aistudio/123/BA/AAML/BAAAML.xaa.sdf\n",
      "/home/aistudio/123/BA/EDMN/BAEDMN.xaa.sdf\n",
      "/home/aistudio/123/BA/BDMO/BABDMO.xaa.sdf\n",
      "/home/aistudio/123/BA/BDRN/BABDRN.xaa.sdf\n",
      "/home/aistudio/123/BA/AAMO/BAAAMO.xaa.sdf\n",
      "/home/aistudio/123/BA/EBRP/BAEBRP.xaa.sdf\n",
      "/home/aistudio/123/BA/CAMN/BACAMN.xaa.sdf\n",
      "/home/aistudio/123/BA/ABMN/BAABMN.xaa.sdf\n",
      "/home/aistudio/123/BA/BBMM/BABBMM.xaa.sdf\n",
      "/home/aistudio/123/BA/CBRO/BACBRO.xaa.sdf\n",
      "/home/aistudio/123/BA/EAMM/BAEAMM.xaa.sdf\n",
      "/home/aistudio/123/BA/BARL/BABARL.xaa.sdf\n"
     ]
    }
   ],
   "source": [
    "#这一段是在编写过程中用print查看了我想要的变量是否正确\n",
    "from pathlib import Path\n",
    "import gzip\n",
    "import shutil\n",
    "\n",
    "mypath = \"/home/aistudio/123\"\n",
    "for p in Path(mypath).iterdir():\n",
    "    for s in p.rglob('*gz'):\n",
    "        y = str(s)\n",
    "        t = y.replace(\".gz\", \"\")\n",
    "        print(t)\n",
    "\n"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "py35-paddle1.2.0"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
